package cn.appsys.controller.back;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.appsys.pojo.AppCategory;
import cn.appsys.pojo.AppInfo;
import cn.appsys.pojo.AppVersion;
import cn.appsys.pojo.DataDictionary;
import cn.appsys.pojo.Page;
import cn.appsys.service.back.AppVersionService;
import cn.appsys.service.back.BackService;
import cn.appsys.service.back.Category_BackService;

import com.alibaba.fastjson.JSONArray;

@Controller
@RequestMapping("/back")
public class BackController {
	@Resource
	private AppVersionService appVersionService;
	@Resource
	private BackService backService;
	@Resource
	private Category_BackService category_BackService;
	@RequestMapping("/main")
	public String main() {
		return "backend/main";
	}
	//分页显示APP待审核查询列表
	@RequestMapping("appList")
	public String appList(
		@RequestParam(value = "querySoftwareName",required = false)String querySoftwareName,
		@RequestParam(value = "queryFlatformId",required = false)Integer queryFlatformId,
		@RequestParam(value = "queryCategoryLevel1",required = false)Integer queryCategoryLevel1,
		@RequestParam(value = "queryCategoryLevel2",required = false)Integer queryCategoryLevel2,
		@RequestParam(value = "queryCategoryLevel3",required = false)Integer queryCategoryLevel3,
		@RequestParam(value = "pageIndex",required = false,defaultValue = "1")Integer pageIndex,Model model) {
		//总记录数
		int totalCount = backService.queryCount(querySoftwareName, queryFlatformId, queryCategoryLevel1, queryCategoryLevel2, queryCategoryLevel3); 
		//page实体类
		Page page = new Page();
		page.setPageSize(5); //显示多少条数据
		page.setCurrPageNo(pageIndex); //当前页
		page.setTotalCount(totalCount);//总记录数
		//分页起始位置偏移量
		Integer from = (page.getCurrPageNo() - 1) * page.getPageSize();
		//分页查询App信息列表
		List<AppInfo> appInfoList = backService.queryAppPageInfo_back(querySoftwareName, queryFlatformId, queryCategoryLevel1, queryCategoryLevel2, queryCategoryLevel3, from, page.getPageSize());
		//所属平台
		List<DataDictionary> flatFormList = backService.dataDictionaryList();
		//一级分类
		List<AppCategory> categoryLevel1List = backService.AppCategoryList1();
		List<AppCategory> categoryLevel2List = null;
		List<AppCategory> categoryLevel3List = null;
		//二级分类
		if(queryCategoryLevel2 != null) {
			categoryLevel2List = backService.AppCategoryList2(queryCategoryLevel2);
		}
		//三级分类
		if(queryCategoryLevel3 != null) {
			categoryLevel3List = backService.AppCategoryList3(queryCategoryLevel3);
		}
		model.addAttribute("flatFormList", flatFormList);
		model.addAttribute("categoryLevel1List", categoryLevel1List);
		model.addAttribute("categoryLevel2List", categoryLevel2List);
		model.addAttribute("categoryLevel3List", categoryLevel3List);
		model.addAttribute("appInfoList", appInfoList);
		model.addAttribute("pages", page);
		 //保存查询条件
		model.addAttribute("querySoftwareName", querySoftwareName);
		model.addAttribute("queryflatformId", queryFlatformId);
		model.addAttribute("queryCategoryLevel1", queryCategoryLevel1);
		model.addAttribute("queryCategoryLevel2", queryCategoryLevel2);
		model.addAttribute("queryCategoryLevel3", queryCategoryLevel3);
		return "backend/applist";
	}
	//二级分类下拉框
	@RequestMapping(value = "/AppCategoryList2",method = RequestMethod.GET)
	@ResponseBody
	public String AppCategoryList2(@RequestParam("pid")Integer pid) {
		List<AppCategory> list = category_BackService.AppCategoryList2(pid);
		return JSONArray.toJSONString(list);
	}
	//三级分类下拉框
		@RequestMapping(value = "/AppCategoryList3",method = RequestMethod.GET)
		@ResponseBody
		public String AppCategoryList3(@RequestParam("pid")Integer pid) {
			List<AppCategory> list = category_BackService.AppCategoryList3(pid);
			return JSONArray.toJSONString(list);
		}
	//App基础信息和最新版本信息
	@RequestMapping("/appShow")
	public String appShow(@RequestParam("aid")Integer aid,@RequestParam("vid")Integer vid,Model model) {
		AppInfo appInfo = backService.appCategoryShow(aid);
		AppVersion appVersion = appVersionService.appVersionShow(vid);
		model.addAttribute(appInfo);
		model.addAttribute(appVersion);
		return"backend/appcheck";
	}
	//是否审核通过
	@RequestMapping("/appStatus")
	public String appStatus(AppInfo appInfo,HttpSession session) {
		int result = backService.upStatus(appInfo);
		if(result > 0) {
			return "redirect:/back/appList";
		}else {
			return "backend/appcheck";
		}
		
	}
}
